﻿using System;
using System.Windows.Forms;

namespace Test_Automation
{
    public partial class Form_control : Form
    {
        public Form_control()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
        private HslCommunication.ModBus.ModbusTcpNet PLC;

        private void Form_control_Load(object sender, EventArgs e)
        {
            SOFT_VER.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            config_json.config_json_readall();

            tb_ip.Text = config_json.PLC_IP;

            X轴当前位置.Text = config_json.X轴当前位置a;
            Y轴当前位置.Text = config_json.Y轴当前位置a;
            Z轴当前位置.Text = config_json.Z轴当前位置a;
            R轴当前位置.Text = config_json.R轴当前位置a;

          
            X轴相对运动坐标写入.Text = config_json.X轴相对运动坐标写入a;
            Y轴相对运动坐标写入.Text = config_json.Y轴相对运动坐标写入a;
            Z轴相对运动坐标写入.Text = config_json.Z轴相对运动坐标写入a;
            R轴相对运动坐标写入.Text = config_json.R轴相对运动坐标写入a;

            X轴复位位置_a.Text = config_json.X轴复位位置a;
            Y轴复位位置_a.Text = config_json.Y轴复位位置a;
            Z轴复位位置_a.Text = config_json.Z轴复位位置a;
            R轴复位位置_a.Text = config_json.R轴复位位置a;

            复位写入_a.Text = config_json.复位写入;


            X轴运行.Text = config_json.X轴相对运动启动a;
            Y轴运行.Text = config_json.Y轴相对运动启动a;
            Z轴运行.Text = config_json.Z轴相对运动启动a;
            R轴运行.Text = config_json.R轴相对运动启动a;

            X轴复位完成_a.Text = config_json.X轴复位完成;
             Y轴复位完成_a.Text = config_json.Y轴复位完成;
            Z轴复位完成_a.Text = config_json.Z轴复位完成;
            R轴复位完成_a.Text = config_json.R轴复位完成;


            //btn_plc_connect_Click(null, null);
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (!System.IO.File.Exists(config_json.config_file_path))
            {
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("d:\\软件配置");
                if (di.Exists == false) di.Create();

                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(config_json.config_file_path, true))
                {
                    writer.WriteLine("{}");
                    writer.Close();
                }
            }
            string json = System.IO.File.ReadAllText(config_json.config_file_path);
            dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
            jsonObj["夹持系统PLC地址"] = tb_ip.Text;

            jsonObj["X轴当前位置"] = X轴当前位置.Text;
            jsonObj["Y轴当前位置"] = Y轴当前位置.Text;
            jsonObj["Z轴当前位置"] = Z轴当前位置.Text;
            jsonObj["分度盘当前位置"] = R轴当前位置.Text;


            jsonObj["X轴相对运动坐标写入"] = X轴相对运动坐标写入.Text;
            jsonObj["Y轴相对运动坐标写入"] = Y轴相对运动坐标写入.Text;
            jsonObj["Z轴相对运动坐标写入"] = Z轴相对运动坐标写入.Text;
            jsonObj["R轴相对运动坐标写入"] = R轴相对运动坐标写入.Text;




            jsonObj["X轴相对运动启动"] = X轴运行.Text;
            jsonObj["Y轴相对运动启动行"] = Y轴运行.Text;
            jsonObj["Z轴相对运动启动"] = Z轴运行.Text;
            jsonObj["R轴相对运动启动"] = R轴运行.Text;



            string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
            System.IO.File.WriteAllText(config_json.config_file_path, output);

            //update config content to variant
            config_json.config_json_readall();

            MessageBox.Show("保存成功！");
        }

        private void btn_plc_connect_Click(object sender, EventArgs e)
        {
            PLC = new HslCommunication.ModBus.ModbusTcpNet(tb_ip.Text, 502, 1);   // 站号1
            PLC.AddressStartWithZero = true;
            PLC.DataFormat = HslCommunication.Core.DataFormat.CDAB;

            HslCommunication.OperateResult resut = PLC.ConnectServer();

            if (resut.IsSuccess)
            {
                tb_log.Text += "已成功连接PLC:"+ tb_ip .Text+ "\r\n";
                btn_plc_connect.Enabled = false;
            }
            else
            {
                tb_log.Text += "连接PLC失败:" + tb_ip.Text +"\r\n";
                btn_plc_connect.Enabled = true;
            }
        }

        private void X轴当前位置w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(X轴当前位置.Text, Convert.ToInt32(X轴当前位置v.Text));
            tb_log.Text += X轴当前位置.Text + "写入" + X轴当前位置v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        private void Y轴当前位置w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(Y轴当前位置.Text, Convert.ToInt32(Y轴当前位置v.Text));
            tb_log.Text += Y轴当前位置.Text + "写入" + Y轴当前位置v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        private void Z轴当前位置w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(Z轴当前位置.Text, Convert.ToInt32(Z轴当前位置v.Text));
            tb_log.Text += Z轴当前位置.Text + "写入" + Z轴当前位置v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        private void R轴当前位置w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(R轴当前位置.Text, Convert.ToInt32(R轴当前位置v.Text));
            tb_log.Text += R轴当前位置.Text + "写入" + R轴当前位置v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        
        private void X轴自动位置w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(X轴相对运动坐标写入.Text, Convert.ToInt32(X轴自动位置v.Text));
            tb_log.Text += X轴相对运动坐标写入.Text + "写入" + X轴自动位置v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        private void Y轴自动位置w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(Y轴相对运动坐标写入.Text, Convert.ToInt32(Y轴自动位置v.Text));
            tb_log.Text += Y轴相对运动坐标写入.Text + "写入" + Y轴自动位置v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        private void Z轴自动位置w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(Z轴相对运动坐标写入.Text, Convert.ToInt32(Z轴自动位置v.Text));
            tb_log.Text += Z轴相对运动坐标写入.Text + "写入" + Z轴自动位置v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        private void R轴自动位置w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(R轴相对运动坐标写入.Text, Convert.ToInt32(R轴自动位置v.Text));
            tb_log.Text += R轴相对运动坐标写入.Text + "写入" + R轴自动位置v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
      
        private void X轴运行w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(X轴运行.Text, Convert.ToInt32(X轴运行v.Text));
            tb_log.Text += X轴运行.Text + "写入" + X轴运行v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        private void Y轴运行w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(Y轴运行.Text, Convert.ToInt32(Y轴运行v.Text));
            tb_log.Text += Y轴运行.Text + "写入" + Y轴运行v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        private void Z轴运行w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(Z轴运行.Text, Convert.ToInt32(Z轴运行v.Text));
            tb_log.Text += Z轴运行.Text + "写入" + Z轴运行v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }
        private void R轴运行w_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(R轴运行.Text, Convert.ToInt32(R轴运行v.Text));
            tb_log.Text += R轴运行.Text + "写入" + R轴运行v.Text;
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void X轴当前位置r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(X轴当前位置.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += X轴当前位置.Text + "读取成功：" +result.Content.ToString() + "\r\n";
                X轴当前位置rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += X轴当前位置.Text + "读取失败\r\n";
            }
        }

        private void Y轴当前位置r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int16> result = PLC.ReadInt16(Y轴当前位置.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += Y轴当前位置.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                Y轴当前位置rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += Y轴当前位置.Text + "读取失败\r\n";
            }
        }
        private void Z轴当前位置r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(Z轴当前位置.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += Z轴当前位置.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                Z轴当前位置rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += Z轴当前位置.Text + "读取失败\r\n";
            }
        }
        private void R轴当前位置r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(R轴当前位置.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += R轴当前位置.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                R轴当前位置rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += R轴当前位置.Text + "读取失败\r\n";
            }
        }
    
        private void X轴自动位置r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(X轴相对运动坐标写入.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += X轴相对运动坐标写入.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                X轴自动位置rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += X轴相对运动坐标写入.Text + "读取失败\r\n";
            }
        }
        private void Y轴自动位置r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(Y轴相对运动坐标写入.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += Y轴相对运动坐标写入.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                Y轴自动位置rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += Y轴相对运动坐标写入.Text + "读取失败\r\n";
            }
        }
        private void Z轴自动位置r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(Z轴相对运动坐标写入.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += Z轴相对运动坐标写入.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                Z轴自动位置rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += Z轴相对运动坐标写入.Text + "读取失败\r\n";
            }
        }
        private void R轴自动位置r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(R轴相对运动坐标写入.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += R轴相对运动坐标写入.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                R轴自动位置rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += R轴相对运动坐标写入.Text + "读取失败\r\n";
            }
        }
        //begin


        private void X轴运行r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(X轴运行.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += X轴运行.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                X轴运行rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += X轴运行.Text + "读取失败\r\n";
            }
        }
        private void Y轴运行r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(Y轴运行.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += Y轴运行.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                Y轴运行rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += Y轴运行.Text + "读取失败\r\n";
            }
        }
        private void Z轴运行r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(Z轴运行.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += Z轴运行.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                Z轴运行rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += Z轴运行.Text + "读取失败\r\n";
            }
        }
        private void R轴运行r_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<Int32> result = PLC.ReadInt32(R轴运行.Text);
            if (result.IsSuccess)
            {
                tb_log.Text += R轴运行.Text + "读取成功：" + result.Content.ToString() + "\r\n";
                R轴运行rv.Text = result.Content.ToString();
            }
            else
            {
                tb_log.Text += R轴运行.Text + "读取失败\r\n";
            }
        }
            
      
        private void button1_Click(object sender, EventArgs e)
        {
            tb_log.Text = "";
        }

        private void btn_readall_Click(object sender, EventArgs e)
        {

        }

       
        private void button5_Click_1(object sender, EventArgs e)
        {

        }

        private void btn_Y轴复位位置_Click(object sender, EventArgs e)
        {

        }

        private void btn_Z轴复位位置_Click(object sender, EventArgs e)
        {

        }

        private void btn_R轴复位位置_Click(object sender, EventArgs e)
        {

        }

        private void btn_reset_Click(object sender, EventArgs e)
        {

        }

        private void btnx_n10_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write( config_json.X轴相对运动坐标写入a, -10000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.X轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnx_n50_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.X轴相对运动坐标写入a, -50000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.X轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnx_p10_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.X轴相对运动坐标写入a, 10000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.X轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnx_p50_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.X轴相对运动坐标写入a, 50000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.X轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btny_p10_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.Y轴相对运动坐标写入a, 10000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.Y轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btny_p50_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.Y轴相对运动坐标写入a, 50000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.Y轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btny_n10_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.Y轴相对运动坐标写入a, -10000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.Y轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btny_n50_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.Y轴相对运动坐标写入a, -50000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.Y轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnz_p10_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.Z轴相对运动坐标写入a, 10000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.Z轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnz_p50_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.Z轴相对运动坐标写入a, 50000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.Z轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnz_n10_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.Z轴相对运动坐标写入a, -10000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.Z轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnz_n50_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.Z轴相对运动坐标写入a, -50000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.Z轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnr_p10_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.R轴相对运动坐标写入a, 10000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.R轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnr_p50_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.R轴相对运动坐标写入a, 50000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.R轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";

        }

        private void btnr_n10_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.R轴相对运动坐标写入a, -10000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.R轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void btnr_n50_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult opresult = PLC.Write(config_json.R轴相对运动坐标写入a, -50000);
            HslCommunication.OperateResult opresult2 = PLC.Write(config_json.R轴相对运动启动a, 1);
            if (opresult.IsSuccess) tb_log.Text += "成功\r\n";
            else tb_log.Text += "失败\r\n";
        }

        private void Z轴当前位置_TextChanged(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {
            X轴复位完成_r.Text += PLC.ReadInt32(X轴复位完成_a.Text).Content.ToString() ;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Y轴复位完成_r.Text += PLC.ReadInt32(Y轴复位完成_a.Text).Content.ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Z轴复位完成_r.Text += PLC.ReadInt32(Z轴复位完成_a.Text).Content.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            R轴复位完成_r.Text += PLC.ReadInt32(R轴复位完成_a.Text).Content.ToString();
        }

        private void btn_save_Click_1(object sender, EventArgs e)
        {
            config_json.save("PLC_IP",tb_ip.Text);
        }
    }
}
